package com.jungle.bfi.service.voucher.impl;

import com.jayway.jsonpath.JsonPath;
import com.jungle.base.dao.SpecBuilder;
import com.jungle.bfi.domain.bo.BizSceneBo;
import com.jungle.bfi.domain.qo.ApiMetadataQo;
import com.jungle.bfi.domain.qo.DataItemQo;
import com.jungle.bfi.entity.*;
import com.jungle.bfi.service.biz.BizFactorService;
import com.jungle.bfi.service.biz.BizSceneService;
import com.jungle.bfi.service.metadata.ApiMetadataService;
import com.jungle.bfi.service.metadata.ApiPathService;
import com.jungle.bfi.service.metadata.DataDefinitionService;
import com.jungle.bfi.service.voucher.VoucherService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VoucherServiceImpl implements VoucherService {

    @Resource
    private ApiPathService apiPathService;
    @Resource
    private ApiMetadataService apiMetadataService;

    @Resource
    private DataDefinitionService dataDefinitionService;

    @Resource
    private BizSceneService bizSceneService;

    @Resource
    private BizFactorService bizFactorService;
    @Override
    public void generateVoucher(String apiPath, String jsonData) {
        log.info("generateVoucher >> apiPath:{}", apiPath);

        // 查询apiPath 信息
        List<ApiPathPo> pathPos = apiPathService.findAll((root, query, cb) -> cb.equal(root.get("path"), apiPath));
        if (pathPos == null || pathPos.isEmpty()) {
            log.info("generateVoucher >> apiPath:{} not found", apiPath);
            return;
        }
        if (pathPos.size() > 1) {
            log.info("generateVoucher >> apiPath:{} found more than one", apiPath);
            return;
        }
        ApiPathPo apiPathPo = pathPos.get(0);

        // 查询接口 元数据信息
        ApiMetadataQo qo = new ApiMetadataQo();
        qo.setApiId(apiPathPo.getApiId());
        qo.setSchemaRef(apiPathPo.getSchemaRef());
        List<ApiMetadataPo> metadataPoList = apiMetadataService.findAllWithChild(qo);
        if (metadataPoList == null || metadataPoList.isEmpty()) {
            log.info("generateVoucher >> apiPath:{} not found metadata", apiPath);
        }

        // 查询接口 数据项信息
        DataItemQo dataItemQo = new DataItemQo();
        dataItemQo.setApiId(apiPathPo.getApiId());
        dataItemQo.setApiPath(apiPathPo.getPath());
        Specification<DataDefinitionPo> spec = new SpecBuilder<DataDefinitionPo>(dataItemQo).toSpec();
        List<DataDefinitionPo> dataDefinitionPoList = dataDefinitionService.findAll(spec);

        // 读取 jsonData，判断 业务场景
        BizSceneBo bizSceneBo = new BizSceneBo();
        bizSceneBo.setApiId(apiPathPo.getApiId());
        bizSceneBo.setApiPath(apiPathPo.getPath());
        Specification<BizScenePo> bizSceneSpec = new SpecBuilder<BizScenePo>(bizSceneBo).toSpec();
        List<BizScenePo> scenePoList = bizSceneService.findAll(bizSceneSpec);

        if (scenePoList == null || scenePoList.isEmpty()) {
            log.info("generateVoucher >> apiPath:{} not found scene", apiPath);
            return;
        }

        // 确定是表单型数据，还是列表型数据

        List<List<BizFactorPo>> factorList = scenePoList.stream().map(po -> bizFactorService.findAll((root, query, cb) -> cb.equal(root.get("sceneId"), po.getId()))).collect(Collectors.toList());

        // TODO 优化查询方法
        // 所有因数条件都满足，则可确定一个场景

        JsonPath jsonPath = JsonPath.compile("");

        Object read = jsonPath.read(jsonData);

    }
}
